package com.leyou.item.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RedisRetryServiceImpl {
    @Autowired
    private  DefaultRedisScript<Long> lockScript;

    @Autowired
    private  DefaultRedisScript<Long> unlockScript;

    @Autowired
    private  StringRedisTemplate redisTemplate;


    private final String LOCK_KEY = "categories:all:lock";

    public Long keyLock(String key){
        List<String> keys = new ArrayList<>();
        keys.add(LOCK_KEY);
        Long result = redisTemplate.execute(lockScript, keys, key);
        log.info("尝试获取lock={}",result);
        return result;
    }

    public Long keyUnlock(String key){
        List<String> keys = new ArrayList<>();
        keys.add(LOCK_KEY);
        Long result = redisTemplate.execute(unlockScript, keys, key);
        log.info("尝试解锁lock={}",result);
        return result;
    }
}
